﻿#define DEBUG_AGENT

using System.Windows;
using Microsoft.Phone.Scheduler;
using System.Collections.Generic;
using Microsoft.Phone.Info;
using Microsoft.Phone.Shell;
using System.Linq;
using System;
using System.IO.IsolatedStorage;

namespace MyScheduledTaskAgent
{
    public class ScheduledAgent : ScheduledTaskAgent
    {
        private static volatile bool _classInitialized;

        /// <remarks>
        /// ScheduledAgent constructor, initializes the UnhandledException handler
        /// </remarks>
        public ScheduledAgent ()
        {
            if (!_classInitialized)
            {
                _classInitialized = true;
                // Subscribe to the managed exception handler
                Deployment.Current.Dispatcher.BeginInvoke (delegate
                {
                    Application.Current.UnhandledException += ScheduledAgent_UnhandledException;
                });
            }
        }

        /// Code to execute on Unhandled Exceptions
        private void ScheduledAgent_UnhandledException (object sender, ApplicationUnhandledExceptionEventArgs e)
        {
            if (System.Diagnostics.Debugger.IsAttached)
            {
                // An unhandled exception has occurred; break into the debugger
                System.Diagnostics.Debugger.Break ();
            }
        }

        /// <summary>
        /// Agent that runs a scheduled task
        /// </summary>
        /// <param name="task">
        /// The invoked task
        /// </param>
        /// <remarks>
        /// This method is called when a periodic or resource intensive task is invoked
        /// </remarks>
        protected override void OnInvoke (ScheduledTask task)
        {
            //TODO: Add code to perform your task in background
            string toastTitle = "";

            // If your application uses both PeriodicTask and ResourceIntensiveTask
            // you can branch your application code here. Otherwise, you don't need to.
            if (task is PeriodicTask)
            {
                // Execute periodic task actions here.
                toastTitle = "Periodic ";
            }
            else
            {
                // Execute resource-intensive task actions here.
                toastTitle = "Resource-intensive ";
            }

            string toastMessage = "Mem usage: " + DeviceStatus.ApplicationPeakMemoryUsage +
                "/" + DeviceStatus.ApplicationMemoryUsageLimit;

            

            // Launch a toast to show that the agent is running.
            // The toast will not be shown if the foreground application is running.
            ShellToast toast = new ShellToast ();
            toast.Title = toastTitle;
            toast.Content = toastMessage;
            toast.Show ();


//            ShellTile TileToFind = ShellTile.ActiveTiles.First ();
//





            if (IsolatedStorageSettings.ApplicationSettings.Contains ("MyTask"))
            {
                List<DateTime> _List = IsolatedStorageSettings.ApplicationSettings["MyTask"] as List<DateTime>;
                DateTime _Now = DateTime.Now;
                DateTime _DateTime = _List[0];

                ShellTile TileToFind = ShellTile.ActiveTiles.First ();

                if (TileToFind != null)
                {
                    StandardTileData NewTileData = new StandardTileData
                    {
                        Title = (_Now.Second-_DateTime.Second).ToString (),
                        
                        // BackgroundImage = new Uri (textBoxBackgroundImage.Text, UriKind.RelativeOrAbsolute),
                        Count = _Now.Second - _DateTime.Second,
                        BackTitle = (_Now.Second - _DateTime.Second).ToString (),
                        //  BackBackgroundImage = new Uri (textBoxBackBackgroundImage.Text, UriKind.Relative),
                        BackContent = "BackContent"

                    };

                    TileToFind.Update (NewTileData);
                }
                //foreach (DateTime _DateTime in _List)
                //{

                //}
            }




            // If debugging is enabled, launch the agent again in one minute.
#if DEBUG_AGENT
            ScheduledActionService.LaunchForTest (task.Name, TimeSpan.FromSeconds (60));
#endif

            // Call NotifyComplete to let the system know the agent is done working.
            NotifyComplete ();
        }
    }
}